/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-15
 * Time: 15:15
 */
public class MySingleList {
    class Node {
        //定于成内部类
        public int val; //存储的数据
        public Node next;//存储下一个节点的地址

        public Node(int val) {
            this.val = val;  //不给next,如果没存的话，不知道下一个节点的地址
        }
    }

    public Node head; //代表当前链表的头结点的引用

    public void createLink() {
        Node node1 = new Node(12);
        Node node2 = new Node(42);
        Node node3 = new Node(31);
        Node node4 = new Node(77);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    //遍历链表
    public void display() {
        Node cur = head;
        //遍历完之后,就不知道链表的head的头结点了 head==null了
        while (cur != null) {  //不能head.next!=null最后一个结点打印不到
            System.out.print(cur.val + " ");
            cur = cur.next; //head往后走
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
        //思考,如果没有head呢 node的next赋为null，变成头节点，则不管有没有,均可以操作
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        //如果没有节点,head为空,cur.next发生空指针异常
    }

    //任意位置插入,第一个数据节点为0下标
    //必须走到前一个位置
    public void addIndex(int index, int data) throws IndexOutOfException {
        CheckIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        Node node = new Node(data);
        Node cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    private void CheckIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexOutOfException("位置不合法");
        }
    }

    //index减1的位置
    private Node findIndexSubOne(int index) {
        Node cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
          if(head == null) {
                return ;//一个节点都没有
            }
            if(head.val == key) {
                head = head.next;
                return;
            }
            Node cur = searchPre(key);
            if(cur == null) {
                return;
            }
            cur.next = cur.next.next;

    }

    //找到key值的前一个节点
    private Node searchPre(int key) {
        if (head == null) {
            return null;
        }
        Node cur = head; //如果没有节点,会发生空指针异常
        while (cur.next != null) { //为什么是cur.next这表示最后一个节点仍然不是删除的值
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null; //没有存在的key值节点
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        /*while(head.val == key) {
            head = head.next;
        }*/
        Node prev = head;
        Node cur = head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) { //此时都删了,就算头节点是,也不要紧,因为其他都删了
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        Node cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空
    //链表中所有的节点都可以被回收
    public void clear() {
       head = null;
    }
}
